import time
from selenium.webdriver.support.select import Select
from Common.Base import open_browser
from Page.LoginPage import LoginPage

"""
author:李孟洋
"""
register_url = 'http://localhost:8080/ecshop/user.php?act=register'
class RegisterPage(LoginPage):
    username_loc = ('id', 'username')
    email_loc = ('id','email')
    password_loc = ('id', 'password1')
    conform_password_loc = ('id','conform_password')
    qq_number_loc = ('name', 'extend_field2')
    office_phone_loc = ('name', 'extend_field3')
    home_phone_loc = ('name','extend_field4')
    mobile_loc = ('name','extend_field5')
    password_prompt_loc = ('name','sel_question')
    password_answer_loc = ('name','passwd_answer')
    agreement_loc = ('name','agreement')
    register_loc = ('css selector','a img[src*="themes/default/images/bnt_ur_re"]')
    submit1_loc = ('name','Submit')

    def input_username(self,username:str):
        """
        注册用户输入账号
        :param username: 用户输入的用户名
        :return:
        """
        if len(username) < 3:
            print('用户名长度不能少于 3 个字符')
        else:
            self.send_keys(self.username_loc,username)

    def input_email(self,email):
        """
        注册用户输入邮箱
        :param email: 邮箱
        :return:
        """
        if email is None:
            print(' 邮件地址不能为空')
        else:
            self.send_keys(self.email_loc,email)

    def input_password(self,password):
        """
        用户输入密码
        :param password1: 密码
        :return:
        """
        self.send_keys(self.password_loc,password)

    def conform_password(self,con_password):
        """
        确认密码
        :param con_password: 输入的密码
        :return:
        """
        self.send_keys(self.conform_password_loc,con_password)

    def qq_number(self,qqnumber=None):
        """
        输入QQ号
        :param qqnumber: QQ号码
        :return:
        """
        self.send_keys(self.qq_number_loc,qqnumber)

    def office_phone(self,phone_num):
        """
        输入办公电话
        :param phone_num: 办公电话号码
        :return:
        """
        self.send_keys(self.office_phone_loc,phone_num)

    def home_phone(self,home_phone):
        """
        输入家庭电话
        :param home_phone: 家庭电话号码
        :return:
        """
        self.send_keys(self.home_phone_loc,home_phone)

    def mobile_phone(self,mobile_num):
        """
        输入手机号码
        :param mobile_num: 手机号
        :return:
        """
        self.send_keys(self.mobile_loc,mobile_num)

    def password_prompt(self,index):
        """
        密码提示问题的选择
        :param index:选择第几个密码提示问题的索引,从0开始
        :return:
        """
        search_box = self.find_element(self.password_prompt_loc)
        Select(search_box).select_by_index(index).click()

    def password_answer(self,text):
        """
        密码问题答案
        :param text:输入的内容
        :return:
        """
        self.send_keys(self.password_answer_loc,text)

    def agreement(self):
        """
        选择接受用户协议
        :return:
        """
        if self.find_element(self.agreement_loc).is_selected():
            pass
        else:
            self.click(self.agreement_loc)

    def click_register(self):
        """
        点击立即注册按钮
        :return:
        """
        self.click(self.submit1_loc)



